﻿namespace SamdomBasicCalculator

open System
open System.Drawing
open System.Windows.Forms


type public MainForm() as form =
    inherit Form()

    // TODO define your controls
    (* Labels *)
    let formLabel = new Label() 
    let lblFirstNum = new Label()
    let lblSecondNum = new Label()
    let lblResult = new Label()
    
    (* Text Boxes *)
    let txtFirstTextBox = new TextBox()
    let txtSecondTextBox = new TextBox()

    (* Buttons *)
    let btnAdd = new Button()
    let btnSubtract = new Button()
    let btnMultiply = new Button()
    let btnDivide = new Button()
    let btnClear = new Button()
    let btnExit = new Button()

    // TODO initialize your controls
    let initControls() = 
        (* FirstNum *)
        lblFirstNum.Text <- "First Number"
        lblFirstNum.Font <- new Font(FontFamily.GenericSerif,12.0f,FontStyle.Bold)
        lblFirstNum.Left <- 100
        lblFirstNum.Top <- 100
        lblFirstNum.Width <- 200
        
        (* SecondNum *)
        lblSecondNum.Text <- "Second Number"
        lblSecondNum.Font <- new Font(FontFamily.Families.[3],12.0f,FontStyle.Bold)
        lblSecondNum.Left <- 450
        lblSecondNum.Top <- 100
        lblSecondNum.Width <- 200

        (* Result Label *)
        lblResult.Left <- 125
        lblResult.Top <- 400
        lblResult.BorderStyle <- BorderStyle.Fixed3D
        lblResult.Width <- 447
        lblResult.Height <- 23
        lblResult.Text <- ""
        (* FirstTextBox *)
        txtFirstTextBox.Left <- 100
        txtFirstTextBox.Top <- 125
        txtFirstTextBox.Width <- 150
        (* SecondTextBox *)
        txtSecondTextBox.Left <- 450
        txtSecondTextBox.Top <- 125
        txtSecondTextBox.Width <- 150


        let buttonFont = SamDomFonts.fetchFontFamily("Bell MT")
        (* Add Button *)
        btnAdd.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnAdd.ForeColor <- Color.Green
        btnAdd.BackColor <- Color.FromArgb(255,255,228,196)
        btnAdd.Text <- "Add"
        btnAdd.Width <- 90
        btnAdd.Height <- 31
        btnAdd.Left <- 100
        btnAdd.Top <- 300
        btnAdd.Click.Add(fun _ ->
                            (* Display *) 
                            lblResult.Text <- SamDomCalculator.add txtFirstTextBox.Text txtSecondTextBox.Text )

        (* Subtract Button *)
        btnSubtract.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnSubtract.ForeColor <- Color.Green
        btnSubtract.BackColor <- Color.FromArgb(255,255,228,196)
        btnSubtract.Text <- "Subtract"
        btnSubtract.Width <- 90
        btnSubtract.Height <- 31
        btnSubtract.Left <- 250
        btnSubtract.Top <- 300
        btnSubtract.Click.Add(fun _ ->
                            (* Display *) 
                            lblResult.Text <- SamDomCalculator.subtract txtFirstTextBox.Text txtSecondTextBox.Text )


        (* Multiply *)
        btnMultiply.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnMultiply.ForeColor <- Color.Green
        btnMultiply.BackColor <- Color.FromArgb(255,255,228,196)
        btnMultiply.Text <- "Multiply"
        btnMultiply.Width <- 90
        btnMultiply.Height <- 31
        btnMultiply.Left <- 400
        btnMultiply.Top <- 300
        btnMultiply.Click.Add(fun _ ->
                            (* Display *) 
                            lblResult.Text <- SamDomCalculator.multiply txtFirstTextBox.Text txtSecondTextBox.Text )

        (* Divide *)
        btnDivide.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnDivide.ForeColor <- Color.Green
        btnDivide.BackColor <- Color.FromArgb(255,255,228,196)
        btnDivide.Text <- "Divide"
        btnDivide.Width <- 90
        btnDivide.Height <- 31
        btnDivide.Left <- 550
        btnDivide.Top <- 300
        btnDivide.Click.Add(fun _ ->
                            (* Display *) 
                            lblResult.Text <- SamDomCalculator.divide txtFirstTextBox.Text txtSecondTextBox.Text )

        (* Clear *)
        btnClear.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnClear.ForeColor <- Color.Green
        btnClear.BackColor <- Color.FromArgb(255,255,228,196)
        btnClear.Text <- "&Clear"
        btnClear.Width <- 90
        btnClear.Height <- 31
        btnClear.Left <- 100
        btnClear.Top <- 475
        btnClear.Click.Add(fun _ ->
                            (* Clear Results *)
                            txtFirstTextBox.Text <- String.Empty
                            txtSecondTextBox.Text <- String.Empty 
                            lblResult.Text <- String.Empty)

        (* Exit *)
        btnExit.Font <- new Font(buttonFont,12.0f,FontStyle.Bold)
        btnExit.ForeColor <- Color.Green
        btnExit.BackColor <- Color.FromArgb(255,255,228,196)
        btnExit.Text <- "&Exit"
        btnExit.Width <- 90
        btnExit.Height <- 31
        btnExit.Left <- 550
        btnExit.Top <- 475
        btnExit.Click.Add(fun _ -> Application.Exit())



        formLabel.DoubleClick.AddHandler(new EventHandler 
            (fun sender e -> form.eventLabel_DoubleClick(sender, e)))   

    do
        form.SuspendLayout();
        initControls()

        // TODO add controls to the form
        //form.Controls.Add(formLabel)
        form.Controls.Add(lblFirstNum)
        form.Controls.Add(lblSecondNum)
        form.Controls.Add(lblResult)
        form.Controls.Add(txtFirstTextBox)
        form.Controls.Add(txtSecondTextBox)
        form.Controls.Add(btnAdd)
        form.Controls.Add(btnSubtract)
        form.Controls.Add(btnMultiply)
        form.Controls.Add(btnDivide)
        form.Controls.Add(btnClear)
        form.Controls.Add(btnExit)
        // TODO define form properties
        form.ClientSize <- new Size(742, 580)
        form.Text <- "SamDom Basic Calculator { F# }"
        form.BackColor <- Color.Olive
        form.Name <- "SamDomBasicCalculator"

        // TODO define any required for events
        form.Load.AddHandler(new System.EventHandler 
            (fun sender e -> form.eventForm_Loading(sender, e)))

        // render the form
        form.ResumeLayout(false)
        form.PerformLayout()

    // TODO define your event handlers
    member form.eventForm_Loading(sender:obj, e:EventArgs) = 
        txtFirstTextBox.Focus() |> ignore (* set the focus to the first number textbox *)

    member form.eventLabel_DoubleClick(sender:obj, e:EventArgs) = 
        ()